<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Controller;

/**
 * Description of Group[
 *
 * @author dongasai
 */
class Group
{

    //put your code here
    /**
     * 获取房间消息
     * @param unknown $room_id
     * @return Ambigous <mixed, unknown>
     */
    protected static function getmesforroom($room_id)
    {
        echo "runing getmesforroom \n";
        $key           = "Room_mes_list_" . $room_id;
        return $Room_mes_list = PSD\Cache::getval($key);
    }

    /**
     * * 添加到客户端房间中
     * @param unknown $room_id
     * @param unknown $client_id
     * @param unknown $client_name
     * @return Ambigous <\PSD\mixed, multitype:unknown >|multitype:unknown
     */
    public static function addClientToRoom($room_id, $client_id, $client_name, $extend)
    {
        $key                  = "ROOM_CLIENT_LIST-$room_id";
        PSD\Cache::getval($key);
        //获取所有在线的client_id
        $all_online_client_id = Gateway::getOnlineStatus();

        $client_list = PSD\Cache::getval($key);
        if (false === $client_list) {
            $client_list = array();
        }

        if (!isset($client_list[$client_id])) {
            // 将存储中不在线用户删除
            if ($all_online_client_id && $client_list) {
                $all_online_client_id = array_flip($all_online_client_id);
                $client_list          = array_intersect_key($client_list, $all_online_client_id);
            }
            // 添加在线客户端
//            echo "\n";
//            var_dump($client_list);
//            echo "\n";
            $client_list[$client_id] = array('name' => $client_name, 'extend' => $extend);
            // 原子添加
            $ret                     = PSD\Cache::setval($key, $client_list);
            return $client_list;
        } else {
            return $client_list;
        }
    }

    /**
     *  剔除 房间
     * @param type $room_id
     * @param type $client_id
     * @return type
     */
    static function roomup($room_id, $client_id)
    {
        echo "runing roomup \n";
        // 删除到当前房间的客户端列表
        $room_clients_list = self::delClientFromRoom($room_id, $client_id);

        // 整理客户端列表以便显示
        $room_clients_list1 = self::formatClientsData($room_clients_list);
        //转播给当前房间的所有客户端，xx进入聊天室 message {type:login, client_id:xx, name:xx}
        //生成消息数组
        $new_message        = array(
            'type'        => 'roomquit',
            'client_id'   => $client_id,
            'client_name' => htmlspecialchars($_SESSION['client_name']),
            'client_list' => $room_clients_list1,
            'time'        => date('Y-m-d H:i:s')
        );

        //生成id列表
        $client_id_array = array_keys($room_clients_list);
        //广播消息
        Gateway::sendToAll(json_encode($new_message), $client_id_array);
        return;
    }

    /**
     * 改变房间
     * @param unknown $client_id
     * @param unknown $room_id
     */
    static function room($client_id, $room_id)
    {

        echo "rooming for $client_id \n";

        if ($_SESSION) {

        } else {
            Gateway::sendToClient($client_id, '{"type":"error","id":"1"}');
            return;
        }
        /**
         *
         * {"type":"login","client_id":88,"client_name":"\u59b9\u59b9","client_list":[
         * {"client_id":84,"info":{"name":"\u4e2a\u4eba","extend":{"headimg":"http:\/\/img5.imgtn.bdimg.com\/it\/u=1686486970,1915201699&fm=23&gp=0.jpg","uid":"6"}}},{"client_id":88,"info":{"name":"\u59b9\u59b9","extend":{"headimg":"http:\/\/img5.imgtn.bdimg.com\/it\/u=2848970854,1449300101&fm=11&gp=0.jpg","uid":"3"}}},{"client_id":"client_online_num","info":2}],"time":"2015-08-12 11:32:59"}
         */
        if (!empty($_SESSION['room_id'])) {
            self::roomup($_SESSION['room_id'], $client_id);
        }
        $_SESSION['room_id'] = $room_id;
        // 存储到当前房间的客户端列表
        $room_clients_list   = self::addClientToRoom($room_id, $client_id, $_SESSION['client_name'], $_SESSION['extend']);
        // 整理客户端列表以便显示
        $room_clients_list1  = self::formatClientsData($room_clients_list);
        //转播给当前房间的所有客户端，xx进入聊天室 message {type:login, client_id:xx, name:xx}
        //生成消息数组
        $new_message         = array(
            'type'        => 'login',
            'client_id'   => $client_id,
            'client_name' => htmlspecialchars($_SESSION['client_name']),
            'client_list' => $room_clients_list1,
            'time'        => date('Y-m-d H:i:s')
        );
        $mes1                = array(
            'type'    => 'return',
            'content' => 'room ok'
        );
        $room_mes_list       = self::getmesforroom($room_id);

        $mes_room_mes_list = array(
            'type'          => 'return',
            'content'       => 'room_mes_list',
            'room_mes_list' => $room_mes_list
        );
        Gateway::sendToClient($client_id, json_encode($mes_room_mes_list));

        Gateway::sendToClient($client_id, json_encode($mes1));

        //生成id列表
        $client_id_array = array_keys($room_clients_list);
        //广播消息
        Gateway::sendToAll(json_encode($new_message), $client_id_array);
        return;
    }

    /**
     * 获得 房间分组的 客户端列表
     * @todo 保存有限个
     */
    public static function getClientListFromRoom($room_id)
    {
        $key = "ROOM_CLIENT_LIST-$room_id";
        $ret = PSD\Cache::getval($key);
        if (false === $ret) {
            return array();
        }
        return $ret;
    }

    /**
     * 给房间内的人发送房间在线列表
     * @version -1
     */
    function sendtoroomidclist()
    {

    }

}
